package SwingView.Quiz;

import SwingModel.CmsQuiz;
import SwingModel.CmsQuizQuestion;
import SwingModel.CmsQuizSubmission;
import SwingModel.CmsStudent;
import SwingView.CmsContentPanelModel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jdesktop.swingx.JXDatePicker;

import SwingController.ALDownloadFile;
import SwingController.CmsPresenter;
import SwingModel.CmsAssignment;
import SwingView.CmsContentPanelModel;
import SwingView.CmsTimeSelector;


public class CmsCPQuizSubmissions extends CmsContentPanelModel {
	
	private CmsQuiz quiz;


	private final JPanel jpContent;
	private JTextArea jtfTitle;
	private JTextArea jtfDescription;
	private List<StudentQuizSubmissionPanel> listOfSubPanel;
	
	private CmsAssignment asgm;
	private JTextArea jtfAvaiDate;
	private JTextArea jtfDueDate;
	private JTextArea jtfPostedDate;
	private JTextArea jtfExpDate;
	private JTextArea jtfWeight;
	private JPanel jpSubmissions;


	private JTextArea jtfTime;


	private JTextArea jtfNumQuestion;


	private List<CmsQuizSubmission> quizSubs;


	private List<CmsStudent> students;
	
	public CmsCPQuizSubmissions(){
		listOfSubPanel = new ArrayList();
		
		//helper panel to center the title panel
		JPanel jp = new JPanel();
		jp.setOpaque(false);
		jp.setPreferredSize(new Dimension(650, 35));
		jp.setBorder(BorderFactory.createEmptyBorder());
		GridBagLayout gbl_jp = new GridBagLayout();
		jp.setLayout(gbl_jp);
		
		//content panel
		jpContent = new JPanel();
		GridBagLayout gbl_jpContent = new GridBagLayout();
		jpContent.setLayout(gbl_jpContent);
		jpContent.setOpaque(false);

		//Set up the scroll bar
		JScrollPane scroll = new JScrollPane(jpContent);
		
		//main Scroll bar
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setOpaque(true);
		scroll.setPreferredSize(new Dimension(650,450));
		scroll.setBackground(Color.WHITE);

		//set up main panel and add components
		setLayout(new BorderLayout());
		setOpaque(false);
		add(jp,BorderLayout.NORTH);//Add title People
		
		//instantiate Quiz title
		JLabel jlAsgmComposerTitle = new JLabel("Quiz Submissions");
		jlAsgmComposerTitle.setFont(jlAsgmComposerTitle.getFont().deriveFont(20.0f).deriveFont(Font.BOLD));
		jlAsgmComposerTitle.setBorder(BorderFactory.createEmptyBorder(0, 8, 10, 8));
		GridBagConstraints gbc_jlQuizComposerTitle = new GridBagConstraints();
		gbc_jlQuizComposerTitle.gridwidth = 2;
		gbc_jlQuizComposerTitle.gridx = 0;
		gbc_jlQuizComposerTitle.gridy = 0;
		jp.add(jlAsgmComposerTitle, gbc_jlQuizComposerTitle);
		
		
		add(scroll,BorderLayout.WEST);//Add People List
		
		//_________________________________________________________
		//TITLE:
		
		JLabel jlTitle = new JLabel("Title: ");
		jlTitle.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_jlTitle = new GridBagConstraints();
		gbc_jlTitle.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_jlTitle.insets = new Insets(0, 40, 5, 5);
		gbc_jlTitle.gridx = 0;
		gbc_jlTitle.gridy = 0;
		jpContent.add(jlTitle, gbc_jlTitle);
		
		jtfTitle = new JTextArea();
		jtfTitle.setToolTipText("Please enter the assignment title");
		jtfTitle.setBorder(BorderFactory.createEmptyBorder());
		GridBagConstraints gbc_jtfTitle = new GridBagConstraints();
		gbc_jtfTitle.insets = new Insets(0, 0, 5, 40);
		gbc_jtfTitle.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_jtfTitle.weightx = 1.0;
		gbc_jtfTitle.gridx = 1;
		gbc_jtfTitle.gridy = 0;
		gbc_jtfTitle.fill = GridBagConstraints.HORIZONTAL;
		jpContent.add(jtfTitle, gbc_jtfTitle);
		
		//_________________________________________________________
		//DESCRIPTION:

		
		JLabel jlDescription = new JLabel("Description: ");
		jlDescription.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_jlDescription = new GridBagConstraints();
		gbc_jlDescription.insets = new Insets(0, 40, 5, 5);
		gbc_jlDescription.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_jlDescription.gridx = 0;
		gbc_jlDescription.gridy = 1;
		jpContent.add(jlDescription, gbc_jlDescription);
		
		jtfDescription = new JTextArea();
		jtfDescription.setLineWrap(true);
		jtfDescription.setWrapStyleWord(true);
		GridBagConstraints gbc_jspDescription = new GridBagConstraints();
		gbc_jspDescription.insets = new Insets(0, 0, 30, 40);
		gbc_jspDescription.fill = GridBagConstraints.HORIZONTAL;
		gbc_jspDescription.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_jspDescription.gridx = 1;
		gbc_jspDescription.gridy = 1;
		gbc_jspDescription.weightx = 1.0;
		jpContent.add(jtfDescription, gbc_jspDescription);
		
		JLabel lblPostedDate = new JLabel("Posted Date:");
		lblPostedDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblPostedDate = new GridBagConstraints();
		gbc_lblPostedDate.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_lblPostedDate.insets = new Insets(0, 40, 5, 5);
		gbc_lblPostedDate.gridx = 0;
		gbc_lblPostedDate.gridy = 2;
		jpContent.add(lblPostedDate, gbc_lblPostedDate);
		
		jtfPostedDate = new JTextArea();
		GridBagConstraints gbc_jtfPostedDate = new GridBagConstraints();
		gbc_jtfPostedDate.insets = new Insets(0, 0, 5, 40);
		gbc_jtfPostedDate.fill = GridBagConstraints.BOTH;
		gbc_jtfPostedDate.gridx = 1;
		gbc_jtfPostedDate.gridy = 2;
		jpContent.add(jtfPostedDate, gbc_jtfPostedDate);
		
		//_________________________________________________________
		//AVAI DATE:
		JLabel lblAvailableDate = new JLabel("Available Date:");
		lblAvailableDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblAvailableDate = new GridBagConstraints();
		gbc_lblAvailableDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblAvailableDate.insets = new Insets(0, 40, 5, 5);
		gbc_lblAvailableDate.gridx = 0;
		gbc_lblAvailableDate.gridy = 3;
		jpContent.add(lblAvailableDate, gbc_lblAvailableDate);
		
		jtfAvaiDate = new JTextArea();
		GridBagConstraints gbc_jtfAvaiDate = new GridBagConstraints();
		gbc_jtfAvaiDate.insets = new Insets(0, 0, 5, 40);
		gbc_jtfAvaiDate.fill = GridBagConstraints.BOTH;
		gbc_jtfAvaiDate.gridx = 1;
		gbc_jtfAvaiDate.gridy = 3;
		jpContent.add(jtfAvaiDate, gbc_jtfAvaiDate);
		
		//_________________________________________________________
		//DUE DATE:
		JLabel lblDueDate = new JLabel("Due Date:");
		lblDueDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblDueDate = new GridBagConstraints();
		gbc_lblDueDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblDueDate.insets = new Insets(0, 40, 5, 5);
		gbc_lblDueDate.gridx = 0;
		gbc_lblDueDate.gridy = 4;
		jpContent.add(lblDueDate, gbc_lblDueDate);
		
		jtfDueDate = new JTextArea();
		GridBagConstraints gbc_jtfDueDate = new GridBagConstraints();
		gbc_jtfDueDate.insets = new Insets(0, 0, 5, 40);
		gbc_jtfDueDate.fill = GridBagConstraints.BOTH;
		gbc_jtfDueDate.gridx = 1;
		gbc_jtfDueDate.gridy = 4;
		jpContent.add(jtfDueDate, gbc_jtfDueDate);
		
		//_________________________________________________________
		//EXP DATE:
		JLabel lblExpirationDate = new JLabel("Expiration Date: ");
		lblExpirationDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblExpirationDate = new GridBagConstraints();
		gbc_lblExpirationDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblExpirationDate.insets = new Insets(0, 40, 5, 5);
		gbc_lblExpirationDate.gridx = 0;
		gbc_lblExpirationDate.gridy = 5;
		jpContent.add(lblExpirationDate, gbc_lblExpirationDate);
		
		jtfExpDate = new JTextArea();
		GridBagConstraints gbc_jtfExpDate = new GridBagConstraints();
		gbc_jtfExpDate.insets = new Insets(0, 0, 5, 40);
		gbc_jtfExpDate.fill = GridBagConstraints.BOTH;
		gbc_jtfExpDate.gridx = 1;
		gbc_jtfExpDate.gridy = 5;
		jpContent.add(jtfExpDate, gbc_jtfExpDate);
		
		JLabel lblTimeConstraint = new JLabel("Time Constraint: ");
		lblTimeConstraint.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblTimeConstraint = new GridBagConstraints();
		gbc_lblTimeConstraint.anchor = GridBagConstraints.LINE_START;
		gbc_lblTimeConstraint.insets = new Insets(0, 40, 5, 5);
		gbc_lblTimeConstraint.gridx = 0;
		gbc_lblTimeConstraint.gridy = 6;
		jpContent.add(lblTimeConstraint, gbc_lblTimeConstraint);
		
		jtfTime = new JTextArea();
		GridBagConstraints gbc_jtfTime = new GridBagConstraints();
		gbc_jtfTime.insets = new Insets(0, 0, 5, 40);
		gbc_jtfTime.fill = GridBagConstraints.BOTH;
		gbc_jtfTime.gridx = 1;
		gbc_jtfTime.gridy = 6;
		jpContent.add(jtfTime, gbc_jtfTime);
		
		JLabel lblOfQuestions = new JLabel("# of Questions");
		lblOfQuestions.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblOfQuestions = new GridBagConstraints();
		gbc_lblOfQuestions.insets = new Insets(0, 40, 5, 5);
		gbc_lblOfQuestions.gridx = 0;
		gbc_lblOfQuestions.gridy = 7;
		jpContent.add(lblOfQuestions, gbc_lblOfQuestions);
		
		jtfNumQuestion = new JTextArea();
		GridBagConstraints gbc_jtfNumQuestion = new GridBagConstraints();
		gbc_jtfNumQuestion.insets = new Insets(0, 0, 5, 40);
		gbc_jtfNumQuestion.fill = GridBagConstraints.BOTH;
		gbc_jtfNumQuestion.gridx = 1;
		gbc_jtfNumQuestion.gridy = 7;
		jpContent.add(jtfNumQuestion, gbc_jtfNumQuestion);
		
		JLabel jlWeight = new JLabel("Percent Weight: ");
		GridBagConstraints gbc_jlWeight = new GridBagConstraints();
		gbc_jlWeight.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_jlWeight.insets = new Insets(0, 40, 5, 5);
		gbc_jlWeight.gridx = 0;
		gbc_jlWeight.gridy = 8;
		jpContent.add(jlWeight, gbc_jlWeight);
		jlWeight.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		
		jtfWeight = new JTextArea();
		GridBagConstraints gbc_jtfWeight = new GridBagConstraints();
		gbc_jtfWeight.anchor = GridBagConstraints.LINE_START;
		gbc_jtfWeight.insets = new Insets(0, 0, 30, 40);
		gbc_jtfWeight.fill = GridBagConstraints.BOTH;
		gbc_jtfWeight.gridx = 1;
		gbc_jtfWeight.gridy = 8;
		jpContent.add(jtfWeight, gbc_jtfWeight);
		
		//Files Label
		JLabel lblFiles = new JLabel("Quiz Questions:");
		GridBagConstraints gbc_lblFiles = new GridBagConstraints();
		gbc_lblFiles.anchor = GridBagConstraints.FIRST_LINE_START;
		gbc_lblFiles.insets = new Insets(0, 40, 5, 5);
		gbc_lblFiles.gridx = 0;
		gbc_lblFiles.gridy = 9;
		jpContent.add(lblFiles, gbc_lblFiles);
		lblFiles.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		
		jpSubmissions = new JPanel(new GridBagLayout());
		jpSubmissions.setOpaque(false);
		GridBagConstraints gbc_jpQuestions = new GridBagConstraints();
		gbc_jpQuestions.insets = new Insets(10, 40, 10, 40);
		gbc_jpQuestions.gridwidth = 2;
		gbc_jpQuestions.weighty = 1.0;
		gbc_jpQuestions.fill = GridBagConstraints.BOTH;
		gbc_jpQuestions.gridx = 0;
		gbc_jpQuestions.gridy = 10;
		jpContent.add(jpSubmissions, gbc_jpQuestions);
		
		
	}
	
	public void displayListOfSubmissions(){
		jpSubmissions.removeAll();
		for(int i=0; i < listOfSubPanel.size(); i++){
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = i;
			c.insets = new Insets(0,0,10,0);
			c.anchor = GridBagConstraints.FIRST_LINE_START;
			c.fill = GridBagConstraints.HORIZONTAL;
			jpSubmissions.add(listOfSubPanel.get(i),c); //add to panel for displaying
			System.out.println(i);
		}
		
		//empty space at bottom - used to fill the bottom of the page
		//when the list is too short
		JPanel p = new JPanel();
		p.setOpaque(false);
		p.setBackground(Color.WHITE);
		GridBagConstraints c_p = new GridBagConstraints();
		c_p.gridy = listOfSubPanel.size();
		c_p.fill = GridBagConstraints.VERTICAL;
		c_p.weightx=1.0;
		c_p.weighty=1.0;
		jpSubmissions.add(p,c_p);
		
		jpSubmissions.revalidate();
				
		jpSubmissions.revalidate();
	}
	
	public void loadQuiz(CmsQuiz quiz, List<CmsQuizSubmission> quizSubs, List<CmsStudent> students){
		this.quiz = quiz;
		this.quizSubs = quizSubs;
		this.students = students;
		resetAllField();
	}
	
	public void resetAllField(){
		
		if(quiz != null){
			this.jtfTitle.setText(quiz.getQuizTitle());
			this.jtfDescription.setText(quiz.getQuizDescription());
			this.jtfPostedDate.setText(CmsPresenter.sdf.format(new Date(quiz.getCreatedDate().getTime())));
			
			//display avaiDate
			Date avaiDate = new Date(quiz.getAvaiDate().getTime());
			if(avaiDate.compareTo(new Date(CmsPresenter.DEFAULT_AVAI_DATE.getTime())) == 0){
				this.jtfAvaiDate.setText("<NONE>");
			} 
			else this.jtfAvaiDate.setText(CmsPresenter.sdf.format(avaiDate));
			
			//display dueDate
			this.jtfDueDate.setText(CmsPresenter.sdf.format(new Date(quiz.getDueDate().getTime())));
			
			//display expDate
			Date expDate = new Date(quiz.getExpDate().getTime());
			if(expDate.compareTo(CmsPresenter.DEFAULT_EXP_DATE) == 0){
				this.jtfExpDate.setText("<NONE>");
			}
			else this.jtfExpDate.setText(CmsPresenter.sdf.format(expDate));
			
			//display time constraint
			if(quiz.getDuration() == 0){
				jtfTime.setText("<NONE>");
			}
			else this.jtfTime.setText("" + quiz.getDuration() + " minutes");
			
			this.jtfNumQuestion.setText("" + quiz.getMaxPoint() + " questions");
			this.jtfWeight.setText("" + quiz.getPercentWeight() + "%");
			
			revalidate();
			repaint();
		}
		
		if(quizSubs != null && students !=null){
			
			listOfSubPanel = new ArrayList();
			if(quizSubs.size() == students.size()){
				for(int i = 0; i< quizSubs.size() && i<students.size(); i++){
					if(students.get(i) == null) System.err.println("Student " + i + "Is null!!!");
					if(quizSubs.get(i) == null) System.err.println("QuizSub  " + i + "Is null!!!");
					listOfSubPanel.add(new StudentQuizSubmissionPanel(students.get(i) , quizSubs.get(i)));
				}
				displayListOfSubmissions();
			}
		}
	}
}
